package com.atguigu.tingshu.config;

import com.atguigu.tingshu.constant.RabbitConstant;
import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * ClassName: ESRabbitConfig
 * Package: com.atguigu.tingshu.config
 * Description:Elasticsearch的rabbitmq的配置
 */
@Configuration
public class ElasticsearchRabbitConfig {
    /**
     * 实例化交换机
     * @return
     */
    @Bean(value = "elasticsearchExchange")
    public Exchange elasticsearchExchange(){
        return ExchangeBuilder.topicExchange(RabbitConstant.ELASTICSEARCH_EXCHANGE).build();
    }

    /**
     * 实例化添加数据的队列
     * @return
     */
    @Bean(value = "elasticsearchAddQueue")
    public Queue elasticsearchAddQueue(){
        return QueueBuilder.durable(RabbitConstant.ELASTICSEARCH_ADD_QUEUE).build();
    }

    /**
     * 实例化删除数据的队列
     * @return
     */
    @Bean(value = "elasticsearchRemoveQueue")
    public Queue elasticsearchRemoveQueue(){
        return QueueBuilder.durable(RabbitConstant.ELASTICSEARCH_REMOVE_QUEUE).build();
    }

    /**
     * 交换机和添加数据的队列绑定
     * @param elasticsearchExchange
     * @param elasticsearchAddQueue
     * @return
     */
    @Bean(value = "exchangeBindingToAddQueue")
    public Binding exchangeBindingToAddQueue(@Qualifier("elasticsearchExchange") Exchange elasticsearchExchange,
                                             @Qualifier("elasticsearchAddQueue") Queue elasticsearchAddQueue){
        return BindingBuilder.bind(elasticsearchAddQueue).to(elasticsearchExchange)
                .with(RabbitConstant.ELASTICSEARCH_ADD_ROUTING).noargs();
    }

    /**
     * 交换机和删除数据的队列绑定
     * @param elasticsearchExchange
     * @param elasticsearchRemoveQueue
     * @return
     */
    @Bean(value = "exchangeBindingToRemoveQueue")
    public Binding exchangeBindingToRemoveQueue(@Qualifier("elasticsearchExchange") Exchange elasticsearchExchange,
                                             @Qualifier("elasticsearchRemoveQueue") Queue elasticsearchRemoveQueue){
        return BindingBuilder.bind(elasticsearchRemoveQueue).to(elasticsearchExchange)
                .with(RabbitConstant.ELASTICSEARCH_REMOVE_ROUTING).noargs();
    }

    /**
     * 实例化删除redis数据的队列
     * @return
     */
    @Bean(value = "albumInfoRemoveQueue")
    public Queue albumInfoRemoveQueue(){
        return QueueBuilder.durable(RabbitConstant.ALBUMINFO_REMOVE_QUEUE).build();
    }

    /**
     * 删除redis数据的队列和交换机绑定
     * @param elasticsearchExchange
     * @param albumInfoRemoveQueue
     * @return
     */
    @Bean
    public Binding exchangeBindingToAlbumInfoRemoveQueue(
            @Qualifier("elasticsearchExchange") Exchange elasticsearchExchange,
            @Qualifier("albumInfoRemoveQueue") Queue albumInfoRemoveQueue){
        return BindingBuilder.bind(albumInfoRemoveQueue).to(elasticsearchExchange)
                .with(RabbitConstant.ALBUMINFO_REMOVE_ROUTING).noargs();
    }

    /**
     * 实例化添加redis数据的队列
     * @return
     */
    @Bean(value = "albumInfoAddQueue")
    public Queue albumInfoAddQueue(){
        return QueueBuilder.durable(RabbitConstant.ALBUMINFO_ADD_QUEUE).build();
    }

    public Binding exchangeBindingToAlbumInfoAddQueue(@Qualifier("elasticsearchExchange") Exchange elasticsearchExchange,
                                                      @Qualifier("albumInfoAddQueue") Queue albumInfoAddQueue){
        return BindingBuilder.bind(albumInfoAddQueue).to(elasticsearchExchange)
                .with(RabbitConstant.ALBUMINFO_ADD_ROUTING).noargs();
    }

}
